#include "..\..\Header\Arme\roquette.h"

using namespace std;

Roquette::Roquette()
{
    Option m_option;
    if(!m_bufferexplosion.loadFromFile("sounds/explosion.ogg"))
    {
        cout << "Impossible de charger le son de l'explosion" << endl;
    }
    else
    {
        m_soundexplosion.setBuffer(m_bufferexplosion);
        m_soundexplosion.setVolume(m_option.GetVolume()/2);
    }
    grav  = 0.0981;
    pi  = Pi;
    m_puissance = 0;
    m_t=0;
    enTire = finTire = false;
    m_image = new sf::Texture();
    m_explimage = new sf::Texture();
    m_degat = 0;
    anim = 0;
    int id(0);
    int j(0), i(0);
    if(!m_explimage->loadFromFile("Picture/map/explosion.png"))
    {
        cout << "Impossible d'ouvrir le fichier" << endl;
    }
    for(j=0;j<4;j++)
    {
        for(i=0;i<4;i++)
        {
            explosion[id].setTexture(*m_explimage);
            explosion[id].setTextureRect(sf::IntRect(64*i,64*j,64+64*i,64+64*j));
            explosion[id].setOrigin(32,32);
            id++;
        }
    }
}
Roquette::Roquette(int explosiona, std::string image, int degat)
{
    Option m_option;
    if(!m_bufferexplosion.loadFromFile("sounds/explosion.ogg"))
    {
        cout << "Impossible de charger le son de l'explosion" << endl;
    }
    else
    {
        m_soundexplosion.setBuffer(m_bufferexplosion);
        m_soundexplosion.setVolume(m_option.GetVolume()/2);
    }
    grav  = 0.15;
    pi  = Pi;
    m_puissance = 0;
    m_t=0;
    enTire = finTire = false;
    m_image = new sf::Texture();
    m_explimage = new sf::Texture();
    m_degat = 0;
    anim = 0;
    int id(0);
    int j(0), i(0);
    if(!m_explimage->loadFromFile("Picture/map/explosion.png"))
    {
        cout << "Impossible d'ouvrir le fichier" << endl;
    }
    for(j=0;j<4;j++)
    {
        for(i=0;i<4;i++)
        {
            explosion[id].setTexture(*m_explimage);
            explosion[id].setTextureRect(sf::IntRect(64*i,64*j,64+64*i,64+64*j));
            explosion[id].setOrigin(32,32);
            id++;
        }
    }
    m_explosion = explosiona;
    if(!m_image->loadFromFile(image))
    {
        cout << "Impossible d'ouvrir le fichier" << endl;
    }
    m_image->copyToImage().saveToFile("Picture/charger.png");
    sf::Sprite *sprite_tmp = new sf::Sprite();
    sprite_tmp->setTexture(*m_image);
    m_munition = *sprite_tmp;
    delete sprite_tmp;
    m_munition.setTexture(*m_image);
    m_munition.setOrigin(m_munition.getLocalBounds().width / 2, m_munition.getLocalBounds().height / 2);
    m_degat = degat;
}
Roquette::~Roquette()
{
}
void Roquette::tirer(float puissance, float angle, sf::Vector2f position, sf::Vector2f vent, bool flip)
{
    m_angle = angle*(3.14/180);
    enTire = true;
    m_flip = flip;
    m_puissance= puissance;
    positioninitial = position;
    m_t = 0;
    m_position = position;
    vitesseinitial.x = cos(m_angle) * m_puissance;
    vitesseinitial.y = sin(m_angle) * m_puissance;
}
void Roquette::deplacer(sf::Vector2f vent, Terrain *terre, Game *jeu, std::vector<sf::Sprite> unite)
{
    if(enTire == true)
    {
        m_t++;
        int y,x;
        float temp_angle;
        if(m_flip)
        {
            m_position.x = (int)(positioninitial.x - vitesseinitial.x*cos(m_angle)*m_t);
            m_position.y = (int)(positioninitial.y - vitesseinitial.y*cos(m_angle)*m_t*(-1) + (grav*m_t*m_t)/2);
            y = (int)(positioninitial.y - vitesseinitial.y*cos(m_angle)*(m_t+1)*(-1) + (grav*(m_t+1)*(m_t+1))/2);
            x = (int)(positioninitial.x - vitesseinitial.x*cos(m_angle)*(m_t-1));
            if(y > m_position.y)
                temp_angle = -acos((m_position.x-x)/sqrt(((m_position.x-x)*(m_position.x-x))+((y-m_position.y)*(y-m_position.y))));
            else if(y <= m_position.y)
                temp_angle = acos((m_position.x-x)/sqrt(((m_position.x-x)*(m_position.x-x))+((y-m_position.y)*(y-m_position.y))));

        }
        else
        {
            m_position.x = (int)(vitesseinitial.x*cos(m_angle)*m_t + positioninitial.x);
            m_position.y = (int)(vitesseinitial.y*cos(m_angle)*m_t*(-1) + positioninitial.y + (grav*m_t*m_t)/2);
            y = (int)(vitesseinitial.y*cos(m_angle)*(m_t+1)*(-1) + positioninitial.y + (grav*(m_t+1)*(m_t+1))/2);
            x = (int)(vitesseinitial.x*cos(m_angle)*(m_t+1) + positioninitial.x);
            if(y > m_position.y)
                temp_angle = -acos((x-m_position.x)/sqrt(((x-m_position.x)*(x-m_position.x))+((y-m_position.y)*(y-m_position.y))));
            else if(y <= m_position.y)
                temp_angle = acos((x-m_position.x)/sqrt(((x-m_position.x)*(x-m_position.x))+((y-m_position.y)*(y-m_position.y))));

        }
            m_munition.setRotation(temp_angle*180/3.14);

            //m_position.x += (vent.x*(m_t)*cos(vent.x/vent.y));
            //m_position.y -= (vent.y*(m_t)*sin(vent.x/vent.y));
        for(int i(0); i<unite.size(); i++)
        {
            if(CollisionBoiteSensible(m_munition, unite[i]) != -1 && m_t > 10)
            {
                jeu->degat(m_explosion, m_position, m_degat, false, 0, false);
                jeu->finTire();
                enTire = false;
                finTire = true;
                time = 0;
                positionexplode = m_position;
                for(int i(0);i<16;i++)
                    explosion[i].setPosition(positionexplode);
            }

        }
        if(terre->contact(m_position))
        {
            jeu->degat(m_explosion, m_position, m_degat, false , 0, false);
            jeu->finTire();
            enTire = false;
            finTire = true;
            time = 0;
            positionexplode = m_position;
            for(int i(0);i<16;i++)
                explosion[i].setPosition(positionexplode);
        }
        m_munition.setPosition(m_position);
        if(m_position.x >= terre->GetTextureSize().x-m_munition.getLocalBounds().height)
        {
            enTire = false;
            jeu->degat(m_explosion, m_position, m_degat, false, 0, false);
            jeu->finTire();
        }
        if(finTire)
            m_soundexplosion.play();
    }
}
void Roquette::afficherRoquette(sf::RenderWindow *app)
{
    if(enTire == true)
    {
        app->draw(m_munition);
    }
    if(finTire == true && clock.getElapsedTime().asSeconds()-time > 0.01)
    {
        time = clock.getElapsedTime().asSeconds();
        app->draw(explosion[anim]);
        anim++;
        if(anim == 16)
        {
            anim = 0;
            finTire = false;
        }
    }
    else if(finTire == true)
    {
        app->draw(explosion[anim]);
    }
}
Roquette& Roquette::operator=(const Roquette &roquette2)
{
    m_explosion = roquette2.m_explosion;
    m_puissance = roquette2.m_puissance;
    m_munition = roquette2.m_munition;
    m_image = roquette2.m_image;
    m_degat = roquette2.m_degat;
}
void Roquette::setRoquette(int explosion, std::string image, int degat)
{
    m_explosion = explosion;
    if(!m_image->loadFromFile(image))
    {
        cout << "Impossible d'ouvrir le fichier" << endl;
    }
    m_image->copyToImage().saveToFile("Picture/charger.png");
    sf::Sprite *sprite_tmp = new sf::Sprite();
    sprite_tmp->setTexture(*m_image);
    m_munition = *sprite_tmp;
    delete sprite_tmp;
    m_munition.setTexture(*m_image);
    m_munition.setOrigin(m_munition.getLocalBounds().width / 2, m_munition.getLocalBounds().height / 2);
    m_degat = degat;
}
sf::Vector2f Roquette::GetPosition()
{
    return m_position;
}
